package com.cn.xiaonuo.tcrfinance.tcrnews.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.vo.CommonParam;
import com.cn.xiaonuo.tcrfinance.tcrnews.entity.PvArticle;
import com.cn.xiaonuo.tcrfinance.tcrnews.mapper.HpcArticlesMapper;
import com.cn.xiaonuo.tcrfinance.tcrnews.service.HpcArticlesSerive;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HpcArticlesServiceImpl implements HpcArticlesSerive {

    @Autowired
    HpcArticlesMapper hpcArticlesMapper;

    /**
     * 新闻
     */
    @Override
    public BusinessMessage selectArticles(CommonParam commonParam) {
        BusinessMessage message = new BusinessMessage();
        Integer pageNo = commonParam.getPageNo();
        Integer pageSize = commonParam.getPageSize();
        if (null == pageNo) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<PvArticle> pvArticles = hpcArticlesMapper.selectList(new QueryWrapper<PvArticle>().eq("state", 1).orderByDesc("art_sort"));
        PageInfo<PvArticle> pageInfo = new PageInfo<>(pvArticles);
        message.setData(pageInfo);
        message.setSuccess(true);
        message.setMessage("操作成功");
        message.setCode("200");
        return message;
    }

    /**
     * 更新文章
     *
     * @param pvArticle
     */
    @Override
    public BusinessMessage updateArticle(PvArticle pvArticle) {
        BusinessMessage message = new BusinessMessage();
        Integer id = pvArticle.getId();
        PvArticle pa = hpcArticlesMapper.selectById(id);
        if (null == pa) {
            message.setMessage("操作失败");
            message.setSuccess(false);
            message.setCode("-1");
            return message;
        }
        pa.setArtSort(pvArticle.getArtSort());
        pa.setTitle(pvArticle.getTitle());
        pa.setImageUrl(pvArticle.getImageUrl());
        pa.setReferenceUrl(pvArticle.getReferenceUrl());
        pa.setType(pvArticle.getType());
        pa.setAddress(pvArticle.getAddress());
        pa.setAdminId(pvArticle.getAdminId());
        pa.setBriefContent(pvArticle.getBriefContent());
        pa.setContent(pvArticle.getContent());
        pa.setUpdatedAt(new Date());
        pa.setState(1);
        hpcArticlesMapper.updateById(pa);
        message.setSuccess(true);
        message.setMessage("操作成功");
        message.setCode("200");
        return message;
    }

    /**
     * 删除
     *
     * @param pvArticle
     */
    @Override
    public BusinessMessage deleteArticle(PvArticle pvArticle) {
        BusinessMessage message = new BusinessMessage();
        Integer id =pvArticle.getId();
        PvArticle pa = hpcArticlesMapper.selectById(id);
        if (null == pa) {
            message.setMessage("操作失败");
            message.setSuccess(false);
            message.setCode("-1");
            return message;
        }
        pa.setUpdatedAt(new Date());
        pa.setState(0);
        hpcArticlesMapper.updateById(pa);
        message.setSuccess(true);
        message.setMessage("操作成功");
        message.setCode("200");
        return message;
    }

    /**
     * 插入
     */
    @Override
    public BusinessMessage insertArticle(PvArticle pvArticle) {
        BusinessMessage message = new BusinessMessage();
        if(pvArticle==null){
            message.setMessage("操作失败");
            message.setSuccess(false);
            message.setCode("-1");
            return message;
        }
        pvArticle.setState(1);
        hpcArticlesMapper.insert(pvArticle);
        message.setSuccess(true);
        message.setMessage("操作成功");
        message.setCode("200");
        return message;
    }

    /**
     * 查询加入人员审核列表
     */
    @Override
    public BusinessMessage selectAllRegisterinfos(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        String phone = (String) params.get("phone");
        String realName = (String) params.get("realName");
        Integer pageNo = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        if (null == pageNo) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> selectArticles= hpcArticlesMapper.selectAllRegisterinfos(realName, phone);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(selectArticles);
        message.setData(pageInfo);
        message.setSuccess(true);
        message.setMessage("操作成功");
        message.setCode("200");
        return message;
    }
}
